var uploader;
var ue;
$(function() {
	activeNav($("#qiyeMessageSend"));
	
	var bodyHeight = $("#panel-body").height();
	var headHeight = $("#panel-body-head").height();
	var bottomHeight = $("#line-bottom").height();
	var lineH = bodyHeight - (headHeight + bottomHeight + 41);
	$("#line-body").css({height:lineH + "px"});
	
	$(window).resize(function() {
		var bodyHeight = $("#panel-body").height();
		var headHeight = $("#panel-body-head").height();
		var bottomHeight = $("#line-bottom").height();
		var lineH = bodyHeight - (headHeight + bottomHeight + 41);
		$("#line-body").css({height:lineH + "px"});
	});
	
	loadTree();
	loadUploader();
	ue = UE.getEditor('newsContent');
	
	$('#btnSend').click(function(){
		uploader.upload();
	});
	
	$("#msgType").change(function(){
		  if ($(this).val() == 0) {
        	$("#textMsg").show();
        	$("#newsMsg").hide();
        } else if ($(this).val() == 1) {
        	$("#textMsg").hide();
        	$("#newsMsg").show();
        }
	});
	
});
function loadTree(){
	var setting = {
		data: {
			simpleData: {
				enable: true
			}
		},
		async:{
			enable: true,
			dataType: "json",
			type: "post",
			url: ctx + "/admin/qiye/dept/ajaxDeptMembers",
			dataFilter: ajaxDataFilter
		},
		check: {
			enable: true,
			chkStyle: "checkbox",
			chkboxType: { "Y": "ps", "N": "ps" }
		},
		callback : {
			onClick: function(event, treeId, treeNode, clickFlag){
				var treeObj = $.fn.zTree.getZTreeObj(treeId);
				treeObj.cancelSelectedNode();
				if(treeNode.isParent){
					if(!treeNode.open){
						treeObj.expandNode(treeNode, true, false);
					}else{
						treeObj.expandNode(treeNode, false, false);
					}
				}
			},
			onCheck: zTreeOnCheck
		}
	};
	$.fn.zTree.init($("#deptTree"), setting);
}
function ajaxDataFilter(treeId, parentNode, responseData) {
	var rd = new Array();

	if (!responseData) {
		return rd;
	}
	
	$.each(responseData, function (i, n) {
		rd.push({
			id: n.id,
			name : n.name,
			pId : n.parentid,
			value : n.value,
			iconSkin : n.iconSkin,
			userid : n.userid
		});
	});
    return rd;
}
function zTreeOnCheck(event, treeId, treeNode){
	
}
function loadUploader(){
	uploader = WebUploader.create({
	    auto: false,
	    swf : ctx + '/static/plugins/webuploader/Uploader.swf',
		server : ctx + "/api/upload/file",
	    pick: { id : '#filePicker', multiple : false },
	    accept: {
	        title: 'Images',
	        extensions: 'gif,jpg,jpeg,bmp,png',
	        mimeTypes: 'image/*'
	    }
	});
	var t = setInterval(function(){
		var isTrue = $("#filePicker").hasClass('webuploader-container');
		if(isTrue){
			clearInterval(t);
			$("#newsMsg").hide();
			return;
		}
	},100);
	
	uploader.on( 'error', function( type ) {
		switch (type) {
			case 'Q_TYPE_DENIED':
				$.alert({message:'请选择正确的图片类型！'});
				break;
			case 'Q_EXCEED_SIZE_LIMIT':
				$.alert({message:'文件大小不能超过2MB！'});
				break;
			default:
				break;
		}
	});
	
	uploader.on( 'fileQueued', function( file  ) {
		uploaderFlag = true;
		var files = uploader.getFiles();
		if(files.length > 1){
			uploader.reset();
			uploader.addFiles( file );
		}
		uploader.makeThumb( file, function( error, src ) {
			generateImageHtml(src,false);
		});
	});
	
	uploader.on('uploadBeforeSend',function(object ,data , headers ){
		data.folder = "/qiyeMessageImages/";
	});
	
	uploader.on( 'uploadAccept', function( object,ret ) {
		generateImageHtml(ret.filePath,true);
	});
	
	uploader.on( 'uploadFinished', function() {
		var treeObj = $.fn.zTree.getZTreeObj("deptTree");
		var nodes = treeObj.getCheckedNodes(true);
		var userids = new Array();
		var usernames = new Array();
		$.each(nodes, function(i, node){
			if(node.userid){
				userids.push(node.userid);
				usernames.push(node.value);
			}
		});
		target = usernames.join(',');
		userIds = userids.join('|');
		var content = null;
		var title = $('#title').val();
		var picurl = $('#image').val();
		var description = $('#descript').val();
		var type = $('#msgType').val();
		if(type == 0){
			content = $('#textContent').val();
		}
		if(type == 1){
			content = ue.getContent();
		}
		
		$.ajax({
			async : false,
			cache : false,
			dataType : 'JSON',
			type : 'POST',
			url : ctx + '/admin/qiye/message/ajaxSend',
			data : {
				target: target,
				userIds: userIds,
				content: content, 
				title: title,
				picurl: picurl,
				description: description,
				type: type
			},
			beforeSend : function(){
				$('.admin').mask("正在提交...");
			},
			success : function(data) {
				$('.admin').unmask();
				$.alert({message: data.message});
			},
			error : function() {
				$('.admin').unmask();
				$.alert({message: "操作异常，请稍候再试！"});
			}
		});
	});
}
function generateImageHtml(src,server){
	if(src != '') {
		var imgSrc = src;
		if(server){
			imgSrc = ctx + src;
			$('#image').val(src);
		}
		var html = '<img src="'+imgSrc+'" height="100" width="180" />'
		$("#fileList").html(html);
	}
}